clear
close all
clc

fntsize=14;
w=500;
h=w*2/(1+sqrt(5));
col=[0;0.6;0.8;1]*ones(1,3);
corr=0.003;
se=1.96;
printyes=1;

%% Load data
[data,labels]=xlsread('DataForFiguresTables.xlsx','TS_data','A1:S11');
[~,N]=size(labels);
for i=1:N
    assignin('base',labels{1,i},data(:,i));
end

%% Fig. II: Aggregate LS and its quantiles
figure('position',[0 0 w+30 h]);
[haxes,hline1,hline2] = plotyy(year,ls_agg_full,year,[ls_p25_full ls_p50_full ls_p75_full],'plot','plot');
set(hline1,'lineWidth',2,'color',col(1,:))
set(hline2,'lineWidth',2,'color',col(2,:),'LineStyle','--')
set(hline2(2),'lineWidth',2,'color',col(2,:),'LineStyle','-','marker','o','markersize',8,'markerfacecolor',col(2,:))
set(haxes(1),'ycolor',col(1,:))
set(haxes(2),'ycolor',0.6*col(2,:),'layer','top')
grid
set(haxes(1),'ylim',[0.40 0.65],'ytick',(0.40:0.05:0.65),'xtick',1967:5:2012,'xlim',[1967 2012],'ticklabelinterpreter','latex','fontsize',fntsize);
set(haxes(2),'ylim',[0.25 1.00],'ytick',(0.25:0.15:1.00),'xtick',1967:5:2012,'xlim',[1967 2012],'ticklabelinterpreter','latex','fontsize',fntsize)
ylabel(haxes(1),'Aggregate labor share'    ,'fontsize',fntsize,'color',col(1,:),'interpreter','latex')
ylabel(haxes(2),'Establishment labor share','fontsize',fntsize,'color',0.8*col(2,:),'interpreter','latex')
title('Aggregate and micro-level labor shares','fontsize',fntsize,'interpreter','latex')
lgnd=legend([hline1;hline2(2);hline2(1);],{'Aggregate (left axis)';'Median (right axis)';'Top/bottom quartile (right axis)'},'location','sw','fontsize',fntsize,'interpreter','latex');
set(lgnd,'color','white');
if printyes==1
    print('../Figures/Fig_TS_LS_Aggr_Quant.eps','-depsc')
end

%% Fig. IV: Counterfactual LS for initially big establishments
figure('position',[0 0 w h]);
hline = plot(year,[ls_cfBIG1982_strbal ls_agg_strbal]);
set(hline(1),'lineWidth',2,'color',col(2,:))
set(hline(2),'lineWidth',2,'color',col(1,:),'LineStyle','-')
set(gca,'xtick',1967:5:2012,'xlim',[1967 2012],'ylim',[0.3 0.57],'ticklabelinterpreter','latex','fontsize',fntsize)
grid
title('Big-player economy','fontsize',fntsize,'interpreter','latex')
lgnd=legend(hline,{'$\lambda^{BIG}$';'$\lambda^{act}$'},'location','sw','fontsize',fntsize,'interpreter','latex');
set(lgnd,'color','white');
if printyes==1
    print('../Figures/Fig_TS_LS_CF_Big.eps','-depsc')
end

%% Fig. V: Counterfactual LS for initially low labor share establishments
figure('position',[0 0 w h]);
hline = plot(year,[ls_cfSTAR1982_strbal ls_agg_strbal]);
set(hline(1),'lineWidth',2,'color',col(2,:))
set(hline(2),'lineWidth',2,'color',col(1,:),'LineStyle','-')
set(gca,'xtick',1967:5:2012,'xlim',[1967 2012],'ylim',[0.3 0.62],'ticklabelinterpreter','latex','fontsize',fntsize)
grid
title('Balanced panel','fontsize',fntsize,'interpreter','latex')
lgnd=legend(hline,{'$\lambda^{STAR}$';'$\lambda^{act}$'},'location','sw','fontsize',fntsize,'interpreter','latex');
set(lgnd,'color','white');
if printyes==1
    print('../Figures/Fig_TS_LS_CF_Star.eps','-depsc')
end

figure('position',[0 0 w h]);
hline = plot(year,[ls_cfSTAR1982_full ls_agg_full]);
set(hline(1),'lineWidth',2,'color',col(2,:))
set(hline(2),'lineWidth',2,'color',col(1,:),'LineStyle','-')
set(gca,'xtick',1967:5:2012,'xlim',[1967 2012],'ylim',[0.4 0.63],'ytick',0.4:0.04:0.6,'ticklabelinterpreter','latex','fontsize',fntsize)
grid
title('Full panel','fontsize',fntsize,'interpreter','latex')
lgnd=legend(hline,{'$\lambda^{STAR}$';'$\lambda^{act}$'},'location','sw','fontsize',fntsize,'interpreter','latex');
set(lgnd,'color','white');
if printyes==1
    print('../Figures/Fig_TS_LS_CF_STARall.eps','-depsc')
end

%% Fig. C.2: Counterfactual LS for initially low labor share firms
figure('position',[0 0 w h]);
hline = plot(year,[ls_cfSTAR1982_firms_full ls_agg_full]);
set(hline(1),'lineWidth',2,'color',col(2,:))
set(hline(2),'lineWidth',2,'color',col(1,:),'LineStyle','-')
set(gca,'xtick',1967:5:2012,'xlim',[1967 2012],'ylim',[0.4 0.62],'ytick',0.4:0.04:0.6,'ticklabelinterpreter','latex','fontsize',fntsize)
grid
title('Superstar firm economy','fontsize',fntsize,'interpreter','latex')
lgnd=legend(hline,{'$\lambda^{STAR}$';'$\lambda^{act}$'},'location','sw','fontsize',fntsize,'interpreter','latex');
set(lgnd,'color','white');
if printyes==1
    print('../Figures/Fig_TS_LS_CF_STARall_Firms.eps','-depsc')
end


%% Fig. VII
figure('position',[0 0 w h]);
hline = plot(year,[ls_agg_noLL20 ls_agg_full]);
set(hline(2),'lineWidth',2,'color',col(1,:))
set(hline(1),'lineWidth',2,'color',col(2,:),'LineStyle','-')
set(gca,'xtick',1967:5:2012,'xlim',[1967 2012],'ticklabelinterpreter','latex','fontsize',fntsize)
grid
title('Aggregate labor share with and without $LL$ establishments','fontsize',fntsize,'interpreter','latex')
lgnd=legend(hline,{'Non-$LL$ estalishments only';'All establishments'},'location','sw','fontsize',fntsize,'interpreter','latex');
set(lgnd,'color','white');
if printyes==1
    print('../Figures/Fig_TS_LS_NoHP.eps','-depsc')
end

%% Fig. B.1
figure('position',[0 0 w h])
a=area([ls_agg_ww ls_agg_sal ls_agg_ben]);
grid
a(1).FaceColor = [1 1 1];
a(2).FaceColor = col(3,:);
a(3).FaceColor = col(2,:);
% hatchfill2(a(1))
set(gca,'xlim',[1 9],'xticklabel',(1967:5:2007),'ticklabelinterpreter','latex','fontsize',fntsize)
legend([a(3);a(2);a(1)],{'Ancillary labor costs';'Salaries, non-production workers';'Wages, production workers'},'location','sw','fontsize',fntsize,'interpreter','latex');
if printyes==1
    print('../Figures/Fig_TS_LS_AggrComponents.eps','-depsc')
end

%% Fig D.1: Definition of LL cutoffs
figure('position',[0 0 w h]);
hline = plot((1967:5:2012)',[ls_agg_noLL20 ls_agg_noLL10 ls_agg_noLL5 ls_agg_full]);
set(hline(4),'lineWidth',2,'color',col(1,:))
set(hline(1),'lineWidth',2,'color',col(2,:),'LineStyle','-')
set(hline(2),'lineWidth',2,'color',col(3,:),'LineStyle','-' ,'marker','o','markersize',8,'markerfacecolor','w')
set(hline(3),'lineWidth',2,'color',col(3,:),'LineStyle','-' ,'marker','*','markersize',9)
set(gca,'xtick',1967:5:2012,'xlim',[1967 2012],'ylim',[0.4 0.8],'ticklabelinterpreter','latex','fontsize',fntsize)
grid
% title('The aggr. labor share without differently defined $LL$ establishments','fontsize',fntsize,'interpreter','latex')
lgnd=legend(hline,{'$LL$ are bottom 20\%';'$LL$ are bottom 10\%';'$LL$ are bottom 5\%';'All establishments'},'location','sw','fontsize',fntsize,'interpreter','latex');
set(lgnd,'color','white');
if printyes==1
    print('../Figures/Fig_TS_LS_HP20HP10HP5.eps','-depsc')
end

%% Fig. D.2 Permanent vs. transitory LL establishments
figure('position',[0 0 w h]);
hline = plot(year,[ls_agg_noLL20 ls_agg_noPermLL ls_agg_full]);
set(hline(3),'lineWidth',2,'color',col(1,:))
set(hline(1),'lineWidth',2,'color',col(2,:),'LineStyle','-')
set(hline(2),'lineWidth',2,'color',col(3,:),'LineStyle','-' ,'marker','o','markersize',8,'markerfacecolor','w')
set(gca,'xtick',1967:5:2012,'xlim',[1967 2012],'ytick',(0.4:0.05:0.8),'ticklabelinterpreter','latex','fontsize',fntsize)
grid
% title('Aggregate labor share with and without $LL$ establishments','fontsize',fntsize,'interpreter','latex')
lgnd=legend(hline,{'No temporary or permanent $LL$';' No permanent $LL$';'All establishments'},'location','sw','fontsize',fntsize-2,'interpreter','latex');
set(lgnd,'color','white');
if printyes==1
    print('../Figures/Fig_TS_LS_PermVsTempHP.eps','-depsc')
end



%% Fig. B.2: Aggregate LS in Census and BLS data
% BLS labor share computed off the KLEMS multifactor productivity table for
% manufacturing; labor share is computed as
% LS = Cost of Labor/(Value of Production - Cost of Energy - Cost of
% Materials - Purchased Services, billions of current dollars). Data before
% 1987 come from the Superseded historical SIC measures.
[data2,labels]=xlsread('DataForFiguresTables.xlsx','TS_data_ann','A1:C47');
[T,K]=size(data2);
for i=1:K
    assignin('base',labels{i},data2(:,i))
end

figure('position',[0 0 w h]);
[haxes,hline1,hline2] = plotyy(year,ls_cen,year,ls_bls,'plot','plot');
set(hline1(1),'lineWidth',2,'color',col(1,:))
set(hline2(1),'lineWidth',1,'color',0.6*col(2,:),'LineStyle','-','marker','o','markerfacecolor','w')
set(haxes(1),'ylim',[0.38 0.63],'ytick',(0.38:0.05:0.63),'xtick',1967:5:2012,'xlim',[1967 2012],'ticklabelinterpreter','latex','fontsize',fntsize,'ycolor',col(1,:))
set(haxes(2),'ylim',[0.46  0.71],'ytick',(0.46:0.05:0.71),'xtick',1967:5:2012,'xlim',[1967 2012],'ticklabelinterpreter','latex','fontsize',fntsize,'ycolor',col(2,:),'layer','top')
grid
ylabel(haxes(1),'Census labor share','fontsize',fntsize,'color',col(1,:),'interpreter','latex')
ylabel(haxes(2),'BLS labor share'   ,'fontsize',fntsize,'color',col(2,:),'interpreter','latex')
lgnd=legend([hline1;hline2],{'Census (left axis)';'BLS (right axis)'},'location','sw','fontsize',fntsize,'interpreter','latex');
set(lgnd,'color','white');
if printyes==1
    print('../Figures/Fig_TS_LS_AggrBLS.eps','-depsc')
end
